package com.example.fastmybatis.dao.provider;

import com.example.fastmybatis.entity.TUser;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class TUserSqlProvider {

    /**
    * @description:更新用户对象
    * @author:MuJiuTian
    * @createDate: 2019/6/8 13:50
    */
    public String updateUser(Map<String,Object> user){
        SQL sql = new SQL();
        sql.UPDATE("t_user");
        if (user.get("username") != null && user.get("username").toString() != ""){
            sql.SET("username=#{username,jdbcType=VARCHAR}");
        }
        if (user.get("remark") != null && user.get("remark").toString() != ""){
            sql.SET("remark=#{remark}");
        }
        if (user.get("state") != null){
            sql.SET("state=#{state}");
        }
        if (user.get("addTime") != null){
            sql.SET("add_time=#{addTime}");
        }
        if (user.get("money") != null){
            sql.SET("money=#{money}");
        }
        if (user.get("left_money") != null){
            sql.SET("left_money=#{leftMoney}");
        }
        sql.WHERE("id = #{id}");
        return sql.toString();
    }

    /**
    * @description:此方法针对添加user对象的时候，防止有字段为null情况
    * @author:MuJiuTian
    * @createDate: 2019/6/8 13:49
    */
    public String insertUser(TUser user){
        SQL sql = new SQL();
        sql.INSERT_INTO("t_user");
        if (user.getUsername() != null && user.getUsername() !=""){
            sql.VALUES("username","#{username,jdbcType=VARCHAR}");
        }
        //下面的if等我就不写了，参考上面的update就可以
        return sql.toString();
    }

    /**
    * @description:高级查询
    * @author:MuJiuTian
    * @createDate: 2019/6/8 16:59
    */
    public String getOrderUsers(Map<String,Object> map){
        SQL sql = new SQL();
        sql.SELECT("id,username,state");
        sql.FROM("t_user");
        sql.WHERE("isdel = 0");
        sql.ORDER_BY("id");
        return sql.toString();
    }
}
